/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Sales.java
 *
 * Created on Oct 21, 2010, 3:34:45 PM
 */

//PACKAGE NAME
package matchmakingsystem;

//LIB FOR ORACLE
import java.sql.*;

//LIB FOR JOPTIONPANE
import javax.swing.JOptionPane;

//LIB FOR SETTING DIMENSION
import java.awt.Dimension;

/**
 *
 * @author shirni
 */
public class Sales extends javax.swing.JFrame {

    /** Creates new form Sales */
    public Sales() {

    try {
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            Connection con;
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mms","mms");
            System.out.println("DATABASE CONNECTED");

        }//end try

    catch (Exception e) {
            System.out.println("CONNNECT ERROR");
    }//end catch

        initComponents();

        SumKeyPanel.setVisible(false);
        DateRangePanel.setVisible(true);
        jTextField3.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    

//***************************** CODINGS FOR LAYOUT *****************************

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        SumKeyPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        DateRangePanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jComboBox7 = new javax.swing.JComboBox();
        jComboBox6 = new javax.swing.JComboBox();
        jComboBox5 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(java.awt.Color.white);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SALES", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Purisa", 1, 36))); // NOI18N
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setBackground(java.awt.Color.white);
        jButton1.setFont(new java.awt.Font("Purisa", 1, 14));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/sum.gif"))); // NOI18N
        jButton1.setText("   SUM   ");
        jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 170, 240, 30));

        jLabel1.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel1.setText("SUM BY:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALL", "STAFF CODE", "INVOICE CODE", "CUSTOMER CODE" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jPanel1.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, -1, -1));

        SumKeyPanel.setBackground(java.awt.Color.white);
        SumKeyPanel.setOpaque(false);
        SumKeyPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel2.setText("ENTER:");
        SumKeyPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jTextField1MouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTextField1MouseEntered(evt);
            }
        });
        SumKeyPanel.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 0, 150, -1));

        jPanel1.add(SumKeyPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 250, -1));

        DateRangePanel.setBackground(java.awt.Color.white);
        DateRangePanel.setOpaque(false);

        jLabel3.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel3.setText("FROM:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<DD>", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<MM>", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }));

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<YYYY>", "2050", "2049", "2048", "2047", "2046", "2045", "2044", "2043", "2042", "2041", "2040", "2039", "2038", "2037", "2036", "2035", "2034", "2033", "2032", "2031", "2030", "2029", "2028", "2027", "2026", "2025", "2024", "2023", "2022", "2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002", "2001", "2000", "1999", "1998", "1997", "1996", "1995", "1994", "1993", "1992", "1990", "1989", "1988", "1987", "1986", "1985", "1984", "1983", "1982", "1981", "1980", "1979", "1978", "1977", "1976", "1975", "1974", "1973", "1972", "1971", "1970", "1969", "1968", "1967", "1966", "1965", "1964", "1963", "1962", "1961", "1960", "1959", "1958", "1957", "1956", "1955", "1954", "1953", "1952", "1951", "1950", "1949", "1948", "1947", "1946", "1945", "1944", "1943", "1942", "1941", "1940", "1939", "1938", "1937", "1936", "1935", "1934", "1933", "1932", "1931", "1930", "1929", "1928", "1927", "1926", "1925", "1924", "1923", "1922", "1921", "1920", "1919", "1918", "1917", "1916", "1915", "1914", "1913", "1912", "1911", "1910", "1909", "1908", "1907", "1906", "1905", "1904", "1903", "1902", "1901" }));

        jComboBox7.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<YYYY>", "2050", "2049", "2048", "2047", "2046", "2045", "2044", "2043", "2042", "2041", "2040", "2039", "2038", "2037", "2036", "2035", "2034", "2033", "2032", "2031", "2030", "2029", "2028", "2027", "2026", "2025", "2024", "2023", "2022", "2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002", "2001", "2000", "1999", "1998", "1997", "1996", "1995", "1994", "1993", "1992", "1990", "1989", "1988", "1987", "1986", "1985", "1984", "1983", "1982", "1981", "1980", "1979", "1978", "1977", "1976", "1975", "1974", "1973", "1972", "1971", "1970", "1969", "1968", "1967", "1966", "1965", "1964", "1963", "1962", "1961", "1960", "1959", "1958", "1957", "1956", "1955", "1954", "1953", "1952", "1951", "1950", "1949", "1948", "1947", "1946", "1945", "1944", "1943", "1942", "1941", "1940", "1939", "1938", "1937", "1936", "1935", "1934", "1933", "1932", "1931", "1930", "1929", "1928", "1927", "1926", "1925", "1924", "1923", "1922", "1921", "1920", "1919", "1918", "1917", "1916", "1915", "1914", "1913", "1912", "1911", "1910", "1909", "1908", "1907", "1906", "1905", "1904", "1903", "1902", "1901" }));

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<MM>", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }));

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<DD>", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jLabel4.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel4.setText("TO:");

        javax.swing.GroupLayout DateRangePanelLayout = new javax.swing.GroupLayout(DateRangePanel);
        DateRangePanel.setLayout(DateRangePanelLayout);
        DateRangePanelLayout.setHorizontalGroup(
            DateRangePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DateRangePanelLayout.createSequentialGroup()
                .addGroup(DateRangePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(DateRangePanelLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(34, 34, 34)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(DateRangePanelLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(52, 52, 52)
                        .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(98, Short.MAX_VALUE))
        );
        DateRangePanelLayout.setVerticalGroup(
            DateRangePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DateRangePanelLayout.createSequentialGroup()
                .addGroup(DateRangePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(DateRangePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1.add(DateRangePanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 370, 60));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "INVOICE CODE", "INVOICE DATE", "PAYMENT (RM)", "CUSTOMER CODE", "STAFF CODE"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 620, 300));

        jLabel5.setFont(new java.awt.Font("Purisa", 1, 14));
        jLabel5.setText("TOTAL SALES: RM");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 630, -1, -1));

        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jTextField2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        jPanel1.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 630, 150, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, 650, 10));

        jTextField3.setBackground(new java.awt.Color(240, 240, 180));
        jTextField3.setFont(new java.awt.Font("DejaVu Sans Mono", 0, 10));
        jTextField3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField3.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray));
        jPanel1.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 80, 130, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 680, 680));

        jButton2.setBackground(java.awt.Color.white);
        jButton2.setFont(new java.awt.Font("Purisa", 1, 14));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/red-mark.gif"))); // NOI18N
        jButton2.setText("   OK   ");
        jButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(710, 590, 170, 40));

        jButton3.setBackground(java.awt.Color.white);
        jButton3.setFont(new java.awt.Font("Purisa", 1, 14));
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/erase.gif"))); // NOI18N
        jButton3.setText(" CANCEL ");
        jButton3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(710, 650, 170, 40));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/matchmakingsystem/Icons/heart1.jpg"))); // NOI18N
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

    //GLOBAL VARIABLES AND FUNCTIONS
//******************************************************************************

//    SalesMenu salesM = new SalesMenu();
    int isDateError = 0;
    NewTableModel newTable = new NewTableModel();

    public Boolean dateError (String DD, String MM, String YYYY) {
        Boolean error = false;

        if(DD.equals("<DD>") || MM.equals("<MM>") || YYYY.equals("<YYYY>")) {
            error = false;
        }

        else {
            int Day = Integer.parseInt(DD.trim());
            int Year = Integer.parseInt(YYYY.trim());

            if (MM.equals("FEB") || MM.equals("APR")|| MM.equals("JUN")
                                 || MM.equals("SEP")|| MM.equals("NOV")) {

                if(MM.equals("FEB")) {

                    if(Year%4 == 0) {
                        if(Day > 29) {
                            error = true;
                        }
                    }//end if (leap year)

                    else {
                        if(Day > 28) {
                            error = true;
                        }
                    }//end else (not leap year)

                }//end if (FEB)

                else {
                    if (Day > 30) {
                        error = true;
                    }
                }//end else (APR, JUN, SEP, NOV)

            }//end if (FEB, APR, JUN, SEP, NOV)

            else {
                error = false;
            }//end else (JAN, MAR, MAY, JUL, AUG, OCT,DEC)

        }//end else (date is chosen)

        return error;
    }//end of dateError


    public void dynamicTable() {
        jTable1.setModel(newTable);
    }

    public void clearTable() {
        jTable1.repaint();

        Dimension THIS_SIZE = (jScrollPane1.getSize());
        jTable1.setMaximumSize(THIS_SIZE);
        jTable1.setMinimumSize(THIS_SIZE);
        jTable1.setPreferredSize(THIS_SIZE);

        newTable.setDataSize(0, 5);
        newTable.setColumnNameSize();

        newTable.setColumnName(0,"INVOICE CODE");
        newTable.setColumnName(1,"INVOICE DATE");
        newTable.setColumnName(2,"PAYMENT (RM)");
        newTable.setColumnName(3,"CUSTOMER CODE");
        newTable.setColumnName(4,"STAFF CODE");

    }

    public Boolean dateValidate(String fDD, String fMM, String fYYYY, String tDD, String tMM, String tYYYY) {
        Boolean error = false;

        if(fDD.equals("<DD>") || fMM.equals("<MM>") || fYYYY.equals("<YYYY>")
                || tDD.equals("<DD>") || tMM.equals("<MM>") || tYYYY.equals("<YYYY>")) {
            error = false;
        }//end if (date is not entered)

        else if (!fDD.equals("<DD>") && !fMM.equals("<MM>") && !fYYYY.equals("<YYYY>")
                  && !tDD.equals("<DD>") && !tMM.equals("<MM>") && !tYYYY.equals("<YYYY>")){

        int fmonth = 0;
        int tmonth = 0;

        if(fMM.equals("JAN")) {
            fmonth = 1;
        }

        else if(fMM.equals("FEB")) {
            fmonth = 2;
        }

        else if(fMM.equals("MAR")) {
            fmonth = 3;
        }

        else if(fMM.equals("APR")) {
            fmonth = 4;
        }

        else if(fMM.equals("MAY")) {
            fmonth = 5;
        }

        else if(fMM.equals("JUN")) {
            fmonth = 6;
        }

        else if(fMM.equals("JUL")) {
            fmonth = 7;
        }

        else if(fMM.equals("AUG")) {
            fmonth = 8;
        }

        else if(fMM.equals("SEP")) {
            fmonth = 9;
        }

        else if(fMM.equals("OCT")) {
            fmonth = 10;
        }

        else if(fMM.equals("NOV")) {
            fmonth = 11;
        }

        else if(fMM.equals("DEC")) {
            fmonth = 12;
        }

        if(tMM.equals("JAN")) {
            tmonth = 1;
        }

        else if(tMM.equals("FEB")) {
            tmonth = 2;
        }

        else if(tMM.equals("MAR")) {
            tmonth = 3;
        }

        else if(tMM.equals("APR")) {
            tmonth = 4;
        }

        else if(tMM.equals("MAY")) {
            tmonth = 5;
        }

        else if(tMM.equals("JUN")) {
            tmonth = 6;
        }

        else if(tMM.equals("JUL")) {
            tmonth = 7;
        }

        else if(tMM.equals("AUG")) {
            tmonth = 8;
        }

        else if(tMM.equals("SEP")) {
            tmonth = 9;
        }

        else if(tMM.equals("OCT")) {
            tmonth = 10;
        }

        else if(tMM.equals("NOV")) {
            tmonth = 11;
        }

        else if(tMM.equals("DEC")) {
            tmonth = 12;
        }

        if((tMM.equals(fMM)  && tYYYY.equals(fYYYY) && (Integer.parseInt(tDD) < Integer.parseInt(fDD)))
            || (tDD.equals(fDD)  && tMM.equals(fMM) && (Integer.parseInt(tYYYY) < Integer.parseInt(fYYYY)))
            || (tYYYY.equals(fYYYY) && (tmonth < fmonth))
            || (Integer.parseInt(tYYYY) < Integer.parseInt(fYYYY))) {

            error = true;
        }
        }//end else (date is entered)

        return error;
    }

/******************************************************************************/
    //END OF GLOBAL VARIABLES AND FUNCTIONS
  
    //CODINGS FOR COMPONENTS
/******************************************************************************/
    
    //SUM BY BOX - STATE CHANGE
    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged

        jTextField1.setText("");
        jTextField2.setText("");
        jComboBox2.setSelectedItem("<DD>");
        jComboBox3.setSelectedItem("<MM>");
        jComboBox4.setSelectedItem("<YYYY>");
        jComboBox5.setSelectedItem("<DD>");
        jComboBox6.setSelectedItem("<MM>");
        jComboBox7.setSelectedItem("<YYYY>");

        if(jComboBox1.getSelectedItem() == "ALL") {
            SumKeyPanel.setVisible(false);
            DateRangePanel.setVisible(true);
        }

        else if(jComboBox1.getSelectedItem() == "INVOICE CODE"
                ||jComboBox1.getSelectedItem() == "CUSTOMER CODE") {
            SumKeyPanel.setVisible(true);
            DateRangePanel.setVisible(false);
        }

        else {
            SumKeyPanel.setVisible(true);
            DateRangePanel.setVisible(true);
        }
        
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    //SUM BUTTON
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        clearTable();

        String SQL = null;
        Double totSales = 0.00;

        String INV_CODE = null;
        String INV_DATE = null;
        String CUS_PAYMENT = null;
        String CUS_CODE = null;
        String EMP_CODE = null;

        String sumbybox = (String) jComboBox1.getSelectedItem();
        String enterfield = jTextField1.getText().trim();
        String fromDD = (String) jComboBox2.getSelectedItem();
        String fromMM = (String) jComboBox3.getSelectedItem();
        String fromYYYY = (String) jComboBox4.getSelectedItem();
        String toDD = (String) jComboBox5.getSelectedItem();
        String toMM = (String) jComboBox6.getSelectedItem();
        String toYYYY = (String) jComboBox7.getSelectedItem();

        Boolean checkFromDate = dateError(fromDD, fromMM, fromYYYY);
        Boolean checkToDate = dateError(toDD, toMM, toYYYY);
        Boolean checkFnTDate = dateValidate (fromDD, fromMM, fromYYYY, toDD, toMM, toYYYY);

        isDateError = (checkFromDate || checkToDate || checkFnTDate)?1:0;

        if(sumbybox.equals("ALL")) {
            SQL = "SELECT INV_CODE,INV_DATE,CUS_PAYMENT,CUS_CODE,EMP_CODE " +
                  "FROM SALES NATURAL JOIN CUSTOMER WHERE INV_DATE>'" +
                  fromDD+fromMM+fromYYYY + "' AND INV_DATE<'" +
                  toDD+toMM+toYYYY + "'";
        }

        else if(sumbybox.equals("STAFF CODE")) {
            SQL = "SELECT INV_CODE,INV_DATE,CUS_PAYMENT,CUS_CODE,EMP_CODE " +
                  "FROM SALES NATURAL JOIN CUSTOMER WHERE INV_DATE>'" +
                  fromDD+fromMM+fromYYYY + "' AND INV_DATE<'" +
                  toDD+toMM+toYYYY + "' AND EMP_CODE = '" +
                  enterfield + "'";
        }

        else if(sumbybox.equals("INVOICE CODE")) {
            SQL = "SELECT INV_CODE,INV_DATE,CUS_PAYMENT,CUS_CODE,EMP_CODE " +
                  "FROM SALES NATURAL JOIN CUSTOMER WHERE INV_CODE = '"+
                  enterfield + "'";
        }

        else if(sumbybox.equals("CUSTOMER CODE")) {
            SQL = "SELECT INV_CODE,INV_DATE,CUS_PAYMENT,CUS_CODE,EMP_CODE " +
                  "FROM SALES NATURAL JOIN CUSTOMER WHERE CUS_CODE = '"+
                  enterfield + "'";
        }

        if(isDateError == 0
//           && !fromDD.equals("<DD>") && !fromMM.equals("<MM>") && !fromYYYY.equals("<YYYY>")
//           && !toDD.equals("<DD>") && !toMM.equals("<MM>") && !toYYYY.equals("<YYYY>")
           || (jComboBox1.getSelectedItem().equals("INVOICE CODE") || jComboBox1.getSelectedItem().equals("CUSTOMER CODE"))
           ) {

            //draw table
            jTable1.repaint();

            Dimension THIS_SIZE = (jScrollPane1.getSize());
            jTable1.setMaximumSize(THIS_SIZE);
            jTable1.setMinimumSize(THIS_SIZE);
            jTable1.setPreferredSize(THIS_SIZE);

            int noOfRow = 0;
            int noOfCol = 0;

            try {
                DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
                Connection con;
                con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","mms","mms");
                System.out.println("DATABASE CONNECTED");

                Statement stmt = con.createStatement();
                ResultSet rset = stmt.executeQuery(SQL);

                while (rset.next())
                {
                    noOfRow++;
                }

                ResultSetMetaData metadata = rset.getMetaData();
                noOfCol = metadata.getColumnCount();

                newTable.setDataSize(noOfRow, 5);
                newTable.setColumnNameSize();

                DatabaseMetaData meta = con.getMetaData();

                ResultSet rset1 = meta.getColumns(null, null, "SALES", null);

                    newTable.setColumnName(0,"INVOICE CODE");
                    newTable.setColumnName(1,"INVOICE DATE");
                    newTable.setColumnName(2,"PAYMENT (RM)");
                    newTable.setColumnName(3,"CUSTOMER CODE");
                    newTable.setColumnName(4,"STAFF CODE");


                if(newTable.rows==0) {
                    JOptionPane.showMessageDialog(null, "No result found. \nPlease try again.", "ERROR!", JOptionPane.WARNING_MESSAGE);
                }//end if (no records)

                //insert table values
                else {
                    int row=0;
                    int col=0;

                    rset = stmt.executeQuery(SQL);

                    while(rset.next()) {
                        INV_CODE = rset.getString("INV_CODE");
    //                    newTable.setValueAt(INV_CODE, row, 0);
                        newTable.addString(row, col, INV_CODE);
                        col++;

                        INV_DATE = rset.getString("INV_DATE");
    //                    newTable.setValueAt(INV_DATE.substring(0,10), row, 1);
                        newTable.addString(row, col, INV_DATE.substring(0,10));
                        col++;

                        CUS_PAYMENT = rset.getString("CUS_PAYMENT");
    //                    newTable.setValueAt(CUS_PAYMENT, row, 2);
                        newTable.addString(row, col, CUS_PAYMENT);
                        col++;

                        totSales = totSales + Double.parseDouble(CUS_PAYMENT.trim());

                        CUS_CODE = rset.getString("CUS_CODE");
    //                    newTable.setValueAt(CUS_CODE, row, 3);
                        newTable.addString(row, col, CUS_CODE);
                        col++;

                        EMP_CODE = rset.getString("EMP_CODE");
    //                    newTable.setValueAt(EMP_CODE, row, 4);
                        newTable.addString(row, col, EMP_CODE);

                        row++;
                        col=0;
                    }

                    this.dynamicTable();
                    jTextField2.setText(String.valueOf(totSales));

                    System.out.println("RETRIEVE SALES DATA SUCCESS");
                }//end else (records exist)
            }//end try

            catch (Exception e) {
                System.out.println("CONNNECT ERROR");
                JOptionPane.showMessageDialog(null, "No result found. \nPlease try again.", "ERROR!", JOptionPane.WARNING_MESSAGE);
            }//end catch
        }//end if (no date error)

        else if(isDateError == 1
//                || (((fromDD.equals("<DD>") || fromMM.equals("<MM>") || fromYYYY.equals("<YYYY>")
//                || toDD.equals("<DD>") || toMM.equals("<MM>") || toYYYY.equals("<YYYY>")))
//                && (!jComboBox1.getSelectedItem().equals("INVOICE CODE") && !jComboBox1.getSelectedItem().equals("CUSTOMER CODE")))
                ) {
            JOptionPane.showMessageDialog(null, "Invalid date entered. \nPlease try again.", "ERROR!", JOptionPane.WARNING_MESSAGE);
        }//end else if (date error occur)

        else
            JOptionPane.showMessageDialog(null, "No result found. \nPlease try again.", "ERROR!", JOptionPane.WARNING_MESSAGE);

    }//GEN-LAST:event_jButton1ActionPerformed

    //OK BUTTON
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        jTextField1.setText("");
        jTextField2.setText("");
        jComboBox1.setSelectedItem("ALL");
        jComboBox2.setSelectedItem("<DD>");
        jComboBox3.setSelectedItem("<MM>");
        jComboBox4.setSelectedItem("<YYYY>");
        jComboBox5.setSelectedItem("<DD>");
        jComboBox6.setSelectedItem("<MM>");
        jComboBox7.setSelectedItem("<YYYY>");

        clearTable();

    }//GEN-LAST:event_jButton2ActionPerformed

    //CANCEL BUTTON
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        jTextField1.setText("");
        jTextField2.setText("");
        jComboBox1.setSelectedItem("ALL");
        jComboBox2.setSelectedItem("<DD>");
        jComboBox3.setSelectedItem("<MM>");
        jComboBox4.setSelectedItem("<YYYY>");
        jComboBox5.setSelectedItem("<DD>");
        jComboBox6.setSelectedItem("<MM>");
        jComboBox7.setSelectedItem("<YYYY>");

        clearTable();

        new SalesMenu().setVisible(true);
        setVisible(false);

    }//GEN-LAST:event_jButton3ActionPerformed

    //MOUSE ENTERED ENTERFIELD
    private void jTextField1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseEntered
        
        if(jComboBox1.getSelectedItem() == "STAFF CODE") {
            jTextField3.setText("Enter STAFF CODE");
        }
 
        else if(jComboBox1.getSelectedItem() == "INVOICE CODE") {
            jTextField3.setText("Enter INVOICE CODE");
        }
        
        else if(jComboBox1.getSelectedItem() == "CUSTOMER CODE") {
            jTextField3.setText("Enter CUSTOMER CODE");
        }

        jTextField3.setVisible(true);
    }//GEN-LAST:event_jTextField1MouseEntered

    //Mouse EXITED ENTERFIELD
    private void jTextField1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseExited
        jTextField3.setVisible(false);
    }//GEN-LAST:event_jTextField1MouseExited

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sales().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel DateRangePanel;
    private javax.swing.JPanel SumKeyPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JComboBox jComboBox7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

}
